package com.atmaowei.service.impl;

import com.atmaowei.controller.request.TeacherOfTeacherPageRequest;
import com.atmaowei.entity.Course;
import com.atmaowei.entity.Student;
import com.atmaowei.entity.StudentAndGrade;
import com.atmaowei.entity.Teacher;
import com.atmaowei.mapper.TeacherOfTeacherMapper;
import com.atmaowei.service.TeacherOfTeacherService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherOfTeacherServiceImpl implements TeacherOfTeacherService {
    @Autowired
    private TeacherOfTeacherMapper teacherOfTeacherMapper;


    @Override
    public String getCnoByCname(String cname) {
        return teacherOfTeacherMapper.getCnoByCname(cname);
    }

    @Override
    public PageInfo<StudentAndGrade> page(TeacherOfTeacherPageRequest teacherOfTeacherPageRequest) {
        teacherOfTeacherPageRequest.setCname(getCnoByCname(teacherOfTeacherPageRequest.getCname()));

        PageHelper.startPage(teacherOfTeacherPageRequest.getPageNum(), teacherOfTeacherPageRequest.getPageSize());
        List<StudentAndGrade> list = teacherOfTeacherMapper.page(teacherOfTeacherPageRequest);
        PageInfo<StudentAndGrade> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<Course> getCourse(String tno) {
        return teacherOfTeacherMapper.getCourse(tno);
    }

    @Override
    public String getCnameByCno(String cno) {
        return teacherOfTeacherMapper.getCnameByCno(cno);
    }

    @Override
    public Teacher getTeacherInfo(String tno) {
        Teacher teacher = teacherOfTeacherMapper.getTeacherInfo(tno);
        teacher.setCno1(getCnameByCno(teacher.getCno1()));
        teacher.setCno2(getCnameByCno(teacher.getCno2()));
        teacher.setCno3(getCnameByCno(teacher.getCno3()));
        return teacher;
    }

    @Override
    public void update(Teacher teacher) {
        teacherOfTeacherMapper.update(teacher);
    }

    @Override
    public boolean checkPassword(String tno, String password) {
        if (teacherOfTeacherMapper.checkPassword(tno).equals(password)) {
            return true;
        }
        return false;
    }

    @Override
    public void updatePassword(String tno, String password) {
        teacherOfTeacherMapper.updatePassword(tno, password);
    }

    @Override
    public void giveGrade(String sno, String cno, String tno, String grade) {
        cno = getCnoByCname(cno);
        teacherOfTeacherMapper.giveGrade(sno, cno, tno, grade);
    }

}
